<!--用户管理-->
<template>
  <div class="page-container">
    <ex_super_table_page v-bind="option" ref="superTablePage">
    </ex_super_table_page>

    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" width="540px" :close-on-click-modal="false">
      <el-form ref="formEl" :model="formValue" :rules="rules" label-width="120px" class="common-form-panel-div"
        style="padding: 0px 10px; margin-top: 10px; ">

        <el-row>
          <el-col>
            <el-form-item :label="$tt('label_fms_service_code_expense_item_coding')">
              <el-input v-model="formValue.serviceCode"
                :disabled="formValue.id && formValue.id != '' && formValue.isStandard == 'Y'"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col>
            <el-form-item :label="$tt('label_fms_service_code_expense_item_name')">
              <el-input v-model="formValue.serviceName"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col>
            <el-form-item :label="$tt('label_fms_service_code_accounting_subject')">
              <el-select v-model="formValue.subjectId" placeholder="请选择" style="width: 100%;">
                <el-option v-for="item in subjectArr" :key="item.id" :label="item.subjectName" :value="item.id">
                </el-option>
              </el-select>

            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col>
            <el-form-item :label="$tt('label_fms_service_code_expense_item_note')">
              <el-input v-model="formValue.remark"></el-input>
            </el-form-item>
          </el-col>
        </el-row>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{ $tt('label_fms_account_config_cancel') }}</el-button>
        <el-button type="primary" @click="submitForm">{{ $tt('label_fms_account_config_confirm') }}</el-button>
      </div>
    </el-dialog>


  </div>
</template>
<script>

let moduleName = "label_fms_service_code_expense_item_manage"
import superTablePageFunction from '@/mixin/super-table-page-function';

export default {

  mixins: [superTablePageFunction],
  title: moduleName,
  components: {},

  data() {

    let _this = this

    return {
      dialogFormVisible: false,
      dialogTitle: '',
      formValue: {},
      rules: {
        // companyName: [
        //   {required: true, message: '请输入企业名称', trigger: 'blur'},
        //   {min: 3, max: 32, message: '长度在 3 到 32 个字符', trigger: 'blur'}
        // ],
        // companyShortCode: [
        //   {required: true, message: '请输入企业编码', trigger: 'blur'},
        //   {min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur'}
        // ]
      },

      subjectArr: [],
      subjectMap: {}

    };
  },

  watch: {
    'formValue.companyId'(val, old) {

    }
  },

  created() {

    let _this = this

    _this.option = {
      search: {
        model: [
          { label: "label_fms_bank_card_keyword", key: "keyword", },
          {
            label: "label_fms_account_apply_status", key: "status", type: 'select', enum: 'IsAvailableStatusEnum', attrs: { clearable: true }
          }
        ],
      },
      action: [
        {
          label: 'label_fms_service_code_add_expense_item',auth:'fms_config_service_code_edit', attrs: { icon: "el-icon-plus" }, click: () => {
            this.dialogTitle = this.$tt('label_fms_service_code_add_expense_item')
            this.dialogFormVisible = true
            this.formValue = {}
          }
        },
      ],

      table: {
        url: "/fms-api/agent/AgentServiceCostRpc/select.json",
        attrs: {
          'cell-style': ({ row, column, rowIndex, columnIndex }) => {
            if (column.property == 'status') {
              return { color: row.status == 'NORMAL' ? this.$globalCss.successColor : this.$globalCss.infoColor }
            }
          }
        },
        model: [

          {key: 'serviceCode', label: 'label_fms_service_code_expense_item_coding', align: 'left', minWidth: 100},
          {key: 'serviceName', label: 'label_fms_service_code_expense_item_name', align: 'left', minWidth: 100},
          {
            key: 'isStandard', label: 'label_fms_service_code_type', align: 'left', minWidth: 100,
            attrs: {
              formatter: (r, c, v, i) => {
                return v == 'Y' ? this.$tt('label_fms_service_code_standard') : this.$tt('label_fms_service_code_custom')
              }
            }
          },
          {
            key: 'subjectName', label: 'label_fms_service_code_accounting_subject', align: 'left', minWidth: 100
          },
          {
            key: 'status', label: 'label_fms_account_apply_status',
            minWidth: 100,
            attrs: {
              formatter: (r, c, v, i) => {
                return this.$enumShow(v, 'IsAvailableStatusEnum')
              }
            }
          },
          {
            type: 'operate', minWidth: 100,
            operate: [

              {
                label: 'IsAvailableStatusEnum__DISABLED',
                auth:'fms_config_service_code_disable',
                type: "confirm-button",
                message: "label_fms_bank_card_confirm_forbidden",
                url: '/fms-api/agent/AgentServiceCostRpc/disable.json',
                if: (row) => {
                  return row.status == 'ENABLE' && row.isStandard == 'N'
                },
                params: (v) => {
                  return { id: v.id }
                },
                done: ['reload', 'tip'],
              },
              {
                label: 'IsAvailableStatusEnum__ENABLE',
                auth:'fms_config_service_code_disable',

                type: "confirm-button",
                message: "label_fms_bank_card_confirm_enable",
                url: '/fms-api/agent/AgentServiceCostRpc/enable.json',
                if: (row) => {
                  return row.status != 'ENABLE' && row.isStandard == 'N'
                },
                params: (v) => {
                  return { id: v.id }
                },
                done: ['reload', 'tip']
              },
              {
                label: 'label_edit', 
                auth:'fms_config_service_code_edit',
                
                click: (row) => {
                  this.dialogTitle = this.$tt('label_fms_service_code_update_expense_item')
                  this.formValue = JSON.parse(JSON.stringify(row))
                  this.dialogFormVisible = true
                }
              },
              {
                auth:'fms_config_service_code_delete',
                label: 'label_delete',
                type: "confirm-button",
                url: '/fms-api/agent/AgentServiceCostRpc/remove.json',
                if: (row) => {
                  return row.status != 'ENABLE' && row.isStandard == 'N'
                },
                message: 'label_fms_bank_card_confirm_delete',
                params: (v) => {
                  return { id: v.id }
                },
                done: ['reload', 'tip']
              }
            ],
          }
        ]
      }
    }



    _this.$ajax({
      url: '/fms-api/agent/AgentSubjectRpc/selectEnable.json',
      type: 'json',
      data: _this.formValue,
      success: (res) => {
        if (res && res.data && res.data.rows) {
          _this.subjectArr = res.data.rows
          _this.subjectMap = {}
          _this.subjectArr.forEach(e => {
            _this.subjectMap[e.id] = e
          })
        }
      }
    });

  },

  props: {},

  destroyed() {

  },

  methods: {

    submitForm() {

      let _this = this

      _this.$refs['formEl'].validate((valid) => {
        if (valid) {

          let subject = _this.subjectMap[_this.formValue.subjectId]
          if (subject) {
            _this.formValue.subjectId = subject.id
            _this.formValue.subjectCode = subject.subjectCode
            _this.formValue.subjectName = subject.subjectName
          }

          _this.$ajax({
            url: '/fms-api/agent/AgentServiceCostRpc/saveUpdate.json',
            type: 'json',
            data: _this.formValue,
            success: (responseData) => {
              this.$message({
                showClose: true,
                message: '提交成功',
                type: 'success'
              })
              _this.$refs['superTablePage'].reload()
              _this.dialogFormVisible = false
            }
          });

        } else {
          return false;
        }

      });

    }


  }
};

</script>
<style>
.small-btn span {
  font-size: 14px;
}

.small-btn .el-button--small span {
  font-size: 14px;
}
</style>
